package cn.edu.ljl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

//对登录信息进行检验
//根据用户名在数据表中的数据进行搜索来验证用户密码
//并检验验证码


public class login extends HttpServlet {

    public static Integer number=0;
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        request.setCharacterEncoding("UTF-8");
        String user = request.getParameter("user");
        String password = request.getParameter("password");
        String code =request.getParameter("code");


        String compare= null;

        String name="'"+user+"'";
        String sql = "SELECT * FROM sign WHERE nam="+name;



        HttpSession session = request.getSession(true);            //验证码功能检验
        String vCode = (String) session.getAttribute(LoginFiltre.LOGIN_CODE);
        if(vCode==null|| !vCode.equalsIgnoreCase(code)){
            response.sendRedirect("/gittest/login.html");
            return;
        }

        try {
            System.out.println(sql);                               //根据输入的用户名从数据库中进行提取，比较密码一致与否
            List<sign> signs = DBMS.getSigns(sql);
            sign s = signs.get(0);
            compare = s.getPassword();
            if(password.equals(compare)){
                session.setAttribute(LoginFiltre.LOGIN_STATUS,Boolean.TRUE);
                number=number+1;
                System.out.println(number);
                response.sendRedirect("/gittest/user/index1.html");
            }else {
                response.sendRedirect("/gittest/login.html");    //不正确的话就返回登录界面
            }
        } catch (SQLException throwables) {
            response.sendRedirect("/gittest/login.html");      //不存在返回登录界面
//            throwables.printStackTrace();
        }

//        if(user!=null&&password!=null){
//            if(user.equals("user")&&password.equals(compare)){
//                session.setAttribute(LoginFiltre.LOGIN_STATUS,Boolean.TRUE);
//                response.sendRedirect("/gittest/user/list");
//            }else {
//                response.sendRedirect("/gittest/login.html");
//            }
//        }



//        if(user!= null &&password != null){
//            if(user.equals("user")&&password.equals("123456")){
//                HttpSession session = request.getSession(true);
//                session.setAttribute(LoginFiltre.LOGIN_STATUS,Boolean.TRUE);
//                response.sendRedirect("/gittest/user/list");
//            }else {
//                response.sendRedirect("/gittest/login.html");
//            }
//        }
    }
}
